package com.ujiuye.userjson;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.userjson.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/get_obj")
public class GetObjJson extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //相应乱码的处理
        resp.setContentType("test/html;charset=utf-8");
        //给js返回一个json对象
        User u1 = new User(1001,"pgone","男",new int[]{99,97,96});
        //将user转换为json然后相应回页面
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(u1);
        //将json相应回浏览器
        resp.getWriter().print(s);


    }
}
